﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tigera.T_Definition;

namespace Plugin.TradeSharp
{

    public partial class UCHandTrade : UserControl
    {
        public UCHandTrade()
        {
            FormLogin.Login();
            InitializeComponent();
            this.comboBoxStock.DataSource = DefaultStocks;
            this.comboBoxStock.ValueMember = "Code";
            this.comboBoxStock.DisplayMember = "FullName";
            this.DataBindings.Add("Enabled", AuthUserService.Instance, "Logined", false, DataSourceUpdateMode.OnPropertyChanged);


        }

        List<StockInfo> DefaultStocks
        {
            get
            {
                List<StockInfo> infos = new List<StockInfo>();

                StockInfo info1 = new StockInfo();
                info1.Code = "002535";
                info1.Name = "林州重机";
                info1.FullName = info1.Code + "-" + info1.Name;
                infos.Add(info1);


                StockInfo info2 = new StockInfo();
                info2.Code = "510300";
                info2.Name = "沪300ETF";
                info2.FullName = info2.Code + "-" + info2.Name;
                infos.Add(info2);

                StockInfo info3 = new StockInfo();
                info3.Code = "159919";
                info3.Name = "深300ETF";
                info3.FullName = info3.Code + "-" + info3.Name;
                infos.Add(info3);


                StockInfo info4 = new StockInfo();
                info4.Code = "IF1308";
                info4.Name = "股指期货";
                info4.FullName = info4.Code + "-" + info4.Name;
                infos.Add(info4);

                return infos;
            }
        }
        public string Title
        {
            get
            {
                return "手工下单(支持拆单)";
            }
        }

        private void btn_Commit_Click(object sender, EventArgs e)
        {
            FormOrderInfo form = new FormOrderInfo();
            HandOrderInfo info = new HandOrderInfo();
            try
            {
                if (this.comboBoxStock.SelectedValue != null)
                {
                    info.Code = this.comboBoxStock.SelectedValue.ToString();
                }
                else
                {
                    info.Code = this.comboBoxStock.Text;
                }
                if (string.IsNullOrEmpty(textBoxStockNum.Text) || string.IsNullOrEmpty(textBoxStockNum.Text))
                {
                    MessageBox.Show(this, "请输入有效的数量、金额");
                    return;
                }
                info.Price = double.Parse(TextBox_Price.Text);
                info.Amount = int.Parse(textBoxStockNum.Text);
                info.BS = radioButton_Buy.Checked ? EntrustBs.Buy : EntrustBs.Sell;
                info.OC = radioButton_Open.Checked ? OpenClose.Open : OpenClose.Close;

                form.OrderInfo = info;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(this, ex.Message);
                return;

            }

            DialogResult result = form.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic["ope"] = "trade";
                dic["code"] = info.Code;
                dic["price"] = info.Price.ToString();
                dic["amount"] = info.Amount.ToString();
                dic["bs"] = ((int)info.BS).ToString();
                dic["openclose"] = ((int)info.OC).ToString();
                T_AnyObjResponse obj = new T_AnyObjResponse();
                obj.DataType = T_AnyObjResponse.DicType;
                obj.Data = ByHelper.ES(dic);

                AppMgr.Instance.Mqclient.SendAny(obj);
            }


        }

        private void comboBoxStock_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        void textBoxStockNum_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            try
            {

                var price = double.Parse(this.TextBox_Price.Text);
                if (price > 0.3)
                {
                    double money = (price * int.Parse(this.textBoxStockNum.Text));

                    this.textBoxMoney.Text = money.ToString();

                }
            }
            catch (System.Exception ex)
            {

            }
        }


        void textBoxMoney_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {

            try
            {

                var price = double.Parse(this.TextBox_Price.Text);
                if (price > 0.3)
                {
                    int num = (int)(double.Parse(this.textBoxMoney.Text) / price);
                    num = ((int)(num / 100)) * 100;
                    this.textBoxStockNum.Text = num.ToString();

                }
            }
            catch (System.Exception ex)
            {

            }

        }
    }


    class StockInfo
    {
        public string Code
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
        public string FullName
        {
            get;
            set;
        }
    }
}
